package failover

import (
	"context"
	"errors"
	"gitee.com/yimtune/webook/internal/service/sms"
	"log"
)

type RoundRobinFailoverSMSService struct {
	svcs []sms.Service
}

func (f *RoundRobinFailoverSMSService) Send(ctx context.Context, tplId string, args []string, numbers ...string) error {
	for _, svc := range f.svcs { // 每次都从第一个开始轮询 大多请求会在第一个成功 负载不均衡
		err := svc.Send(ctx, tplId, args, numbers...)
		if err == nil {
			return nil
		}
		log.Println(err)
	}
	return errors.New("轮询了所有的服务商，但是发送都失败了")
}

func NewRoundRobinFailoverSMSService(svcs []sms.Service) *RoundRobinFailoverSMSService {
	return &RoundRobinFailoverSMSService{
		svcs: svcs,
	}
}
